package com.mingxi.backend.controller;

import com.mingxi.backend.common.Result;
import com.mingxi.backend.pojo.dto.LoginDTO;
import com.mingxi.backend.pojo.dto.UserInfoDto;
import com.mingxi.backend.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/api/v1/users")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO){
        return userService.login(loginDTO);
    }

    @GetMapping("/info")
    public Result info(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        String value = cookies[0].getValue();
        ArrayList<String> roles = new ArrayList<>();
        roles.add("admin");
        return Result.success(new UserInfoDto(value.split("-")[1], roles));
    }
}
